import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup } from '@angular/forms';
import { FetchService } from '../../../core/net/fetch.service';
import * as moment from 'moment';
import * as _ from 'lodash';
import { NzMessageService } from 'ng-zorro-antd';

@Component({
  selector: 'app-gate-records',
  templateUrl: './gate-records.component.html',
  styleUrls: ['./gate-records.component.scss']
})
export class GateRecordsComponent implements OnInit {
    searchForm: FormGroup;
    tableData: Array<any> = [];
    pageNum = 1;
    pageSize = 15;
    total: number;
    loading = false;

    form: FormGroup;
    modalTitle: string = '';
    modalShow: boolean = false;
    gateList = [];
    constructor(private fb: FormBuilder,
                private message: NzMessageService,
                private fetchService: FetchService) {
        this.searchForm = this.fb.group({
            gateName: [null],
            operator: [null],
            rangePickerTime: [[]],
        });
        this.form = this.fb.group({
            taskid: [null],
            objid: [],
            flow: [],
            waterLevel: [],
            degreee: [],
            operator: [],
            operateTime: [new Date()]
        });
        //  闸门列表查询
        this.fetchService.post('base/wrsBrwaterDam/queryDamInfoList', {pageSize: 10000}).subscribe(res => {
            this.gateList = res['list'];
        });
    }

    ngOnInit(): void {
        this.queryInfo();
    }

    queryInfo(reset: boolean = false): void {
        if (reset) {
            this.pageNum = 1;
        }
        this.loading = true;
        const params = {
            userName: this.searchForm.value.operator,
            watergateCode: this.searchForm.value.gateName,
            pageNum: this.pageNum,
            pageSize: this.pageSize
        };
        if (!!this.searchForm.value.rangePickerTime[0]) {
            _.assign(params, {
                beginTime: moment(this.searchForm.value.rangePickerTime[0]).format(
                    'YYYY-MM-DD'
                ),
                endTime: moment(this.searchForm.value.rangePickerTime[1]).format(
                    'YYYY-MM-DD'
                ),
            });
        }
        this.fetchService.post('base/wrsBawaterGateControl/list', params).subscribe(res => {
            this.tableData = res['list'];
            this.total = res['total'];
            this.loading = false;
        });
    }

    openEditModal(item?): void {
        this.modalTitle = item ? `修改记录` : '新增记录';
        this.form.patchValue(item || {});
        this.modalShow = true;
    }

    onConfirm() {
        if (!this.form.valid) {
            this.message.warning('信息填写有误');
            return;
        }
        const data = {
            ...this.form.value,
           };
        // const url = data.taskid ? 'projectManage/xjTask/edit' : 'projectManage/xjTask/add';
        const url = '';
        this.fetchService.post(url, data).subscribe(res => {
            this.message.success('提交成功');
            this.modalShow = false;
            this.queryInfo();
            this.form.reset();
        });
    }

    onCancel() {
        this.modalShow = false;
        this.form.reset();
    }
}
